import xadmin
from xadmin import views


from . import models
from celery_tasks.html.tasks import generate_static_list_search_html, generate_static_sku_detail_html




class SKUAdmin(object):
    """商品管理"""
    # 图标
    model_icon = 'fa fa-gift'
    # 列表展示内容
    list_display = ['id', 'name', 'price', 'stock', 'sales', 'comments']
    # 只读类
    readonly_fields = ['sales', 'comments']
    # 搜索关键词
    search_fields = ['id', 'name']
    # 过滤器
    list_filter = ['category']
    # 商品列 编辑
    list_editable = ['price', 'stock']
    # 商品详情
    show_detail_fields = ['name']
    # 书签
    show_bookmarks = True

    # list_export = ['xls', 'csv', 'xml']    # 默认有五种选择

    def save_models(self):
        obj = self.new_obj
        obj.save()
        generate_static_sku_detail_html.delay(obj.id)

    def delete_models(self):
        obj = self.obj
        sku_id = obj.sku.id
        obj.delete()
        generate_static_sku_detail_html.delay(id)


xadmin.site.register(models.SKU, SKUAdmin)


class BaseSetting(object):
    """xadmin基本配置"""
    enable_themes = True  # 开启主题切换功能
    use_bootswatch = True  # 使用多主题

xadmin.site.register(views.BaseAdminView, BaseSetting)


class GlobalSettings(object):
    """xadmin全局配置"""
    site_title = '美多商城运营管理系统'  # 设置站点标题
    site_footer = '美多商城集团有限公司'  # 设置站点页脚
    menu_style = 'accordion'  # 设置菜单折叠

xadmin.site.register(views.CommAdminView, GlobalSettings)



xadmin.site.register(models.GoodsCategory)
xadmin.site.register(models.GoodsChannel)

xadmin.site.register(models.Brand)
xadmin.site.register(models.GoodsSpecification)
xadmin.site.register(models.SpecificationOption)
xadmin.site.register(models.SKUSpecification)
xadmin.site.register(models.SKUImage)